﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using Antu.WebControls;
using Antu.Core.Components;
using Antu.Core.Services;
using System.Web.UI.WebControls;
using EluZSProvider;
using System.Web;
using System.Data;

namespace WebSys.UI.Control.ZhengShou
{
    public class ZS_DQRHZINControl : MPBasicTemplate
    {
        EluZSDataProvider _EluZSDataProvider = (EluZSDataProvider)ProviderService.ProviderServiceMange.GetProvider("EluZSProvider");
        CustomGridView CustomGridView1;
        Label Label1;
        TextBox xmmc, lxh;
        Button btnsearch;
        public string userid = string.Empty;
        protected override void OnLoad(EventArgs e)
        {
            if (HttpContext.Current.Session["UserID"] == null)
            {
                Antu.Utilities.UtilitiesPage.AlertDialogClear("登录超时，请重新登录！", "window.parent.location.href('../login.aspx');");
            }
            else
            {
                userid = HttpContext.Current.Session["UserID"].ToString();
            }
            base.OnLoad(e);
            IninControl();
            if (!base.Page.IsPostBack)
            {
                Bind();
            }
            CustomGridView1.OnLoadData += new EventHandler(CustomGridView1_OnLoadData);
            foreach (GridViewRow gvr in CustomGridView1.Rows)
            {
                Button btn_tj = (Button)gvr.FindControl("btn_tj");
                btn_tj.Click += new EventHandler(ChangeStatus);
            }
            btnsearch.Click += new EventHandler(btnsearch_Click);
        }

        void btnsearch_Click(object sender, EventArgs e)
        {
            Bind();
        }
        protected void IninControl()
        {
            CustomGridView1 = (CustomGridView)FindControl("CustomGridView1");
            Label1 = (Label)FindControl("Label1");
            xmmc = (TextBox)FindControl("xmmc");
            lxh = (TextBox)FindControl("lxh");
            btnsearch = (Button)FindControl("btnsearch");
        }
        protected void Bind()
        {
            CustomGridView1.RowCreated += new GridViewRowEventHandler(CustomGridView1_RowCreated);
            CustomGridView1.RowDataBound += new GridViewRowEventHandler(CustomGridView1_RowDataBound);
            DataTable dt = _EluZSDataProvider.GetDHZJE(GetStrWhere());
            CustomGridView1.DataSource = dt;
            CustomGridView1.RecordCount = dt.Rows.Count;
            CustomGridView1.DataKeyNames = new string[] { "pcid", "提交状态" };
            CustomGridView1.DataBind();
        }
        protected string GetStrWhere()
        {
            string strWhere = string.Empty;
            if (!string.IsNullOrEmpty(xmmc.Text))
            {
                strWhere += " and t.pnid in (select id from zs_prono where pid in (select id from zs_project where 项目名称 like '%" + xmmc.Text.Trim() + "%'))";
            }
            if (!string.IsNullOrEmpty(lxh.Text))
            {
                strWhere += " and t.pnid in (select id from zs_prono where pid in (select id from zs_project where 立项号 like '%" + lxh.Text.Trim() + "%'))";
            }
            return strWhere;
        }
        protected void CustomGridView1_OnLoadData(object sender, EventArgs e)
        {
            Bind();
        }

        protected void CustomGridView1_RowCreated(object sender, GridViewRowEventArgs e)
        {
            foreach (GridViewRow gvr in CustomGridView1.Rows)
            {
                if (CustomGridView1.DataKeys[gvr.RowIndex].Values["提交状态"].ToString() == "1")
                {
                    Button btn = (Button)gvr.FindControl("btn_tj");
                    btn.Enabled = true;
                }
            }
        }
        protected void CustomGridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                string strPCID = ((HiddenField)e.Row.FindControl("HiddenField1")).Value;
                string tt = _EluZSDataProvider.GetSomeStr("zs_zspch", "sum(to_number(nvl(划转金额,0)))", "pcid", strPCID);
                e.Row.Cells[7].Text = tt;
            }
        }
        protected void ChangeStatus(object sender, EventArgs e)
        {
            Button btn = sender as Button;
            GridViewRow gvr = btn.Parent.Parent as GridViewRow;
            string pcID = CustomGridView1.DataKeys[gvr.RowIndex].Values["pcid"].ToString();
            bool change = _EluZSDataProvider.UpdateBSZT("zs_zspch", "提交状态", "2", "pcid", pcID);
            if (change)
            {
                Bind();
            }
        }
    }
}


